<template>
  <el-dialog
    :model-value="dialogVisible"
    :title="dialogTitle"
    width="50%"
    @close="handleClose"
  >
    <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
      <el-form-item label="车主姓名" prop="checkinId">
        <el-input v-model="form.checkin.name" disabled />
      </el-form-item>
      <el-form-item label="保养/维修" prop="checkinId">
        <el-select v-model="form.checkin.type" clearable placeholder="请选择" disabled>
          <el-option value="2" label="保养">保养</el-option>
          <el-option value="1" label="维修">维修</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="维修保养项目" prop="maintainId">
        <el-table :data="store.getters.getGenerate" row-key border style="width: 100%" @select="select">
          <el-table-column prop="name"></el-table-column>
        </el-table>
      </el-form-item>
      <el-button type="primary" @click="maintenance">+ 选择维修保养项目</el-button>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleConfirm">添加</el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>
    <Appointment v-model="appointmentVisible"
                 :appointmentTitle="appointmentTitle"
                 v-if="appointmentVisible"
                 :appointmentRowValue="appointmentRowValue" />
  </el-dialog>
</template>

<script setup>
import { defineEmits, ref, defineProps, watch, onMounted } from 'vue'
// import { modify } from '../../../api/checkin'
import { getGenerate } from '../../../api/maintain'
import { save } from '../../../api/maintainDetail'
import Appointment from './appointment'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'

const form = ref({
  maintainRecordId: '',
  maintainId: ''
})

const appointmentVisible = ref(false)
const appointmentTitle = ref('')
const appointmentRowValue = ref({})

// 是否保养项目
function maintenance() {
  appointmentTitle.value = '选择保养项目'
  appointmentRowValue.value = {}
  appointmentVisible.value = true
}

const store = useStore()

const props = defineProps({
  // 标题
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  // 编辑行数据
  dialogRowValue: {
    type: Object,
    default: () => {}
  }
})
// 监听props.dialogRowValue值改变
watch(
  () => props.dialogRowValue,
  () => {
    form.value = props.dialogRowValue
  },
  { deep: true, immediate: true }
)

const isDisabled = ref(false)
onMounted(() => {
  if (props.dialogTitle === '编辑') {
    isDisabled.value = true
  }
})

// 表单校验
const rules = ref({
  name: [
    { required: true, message: '职位不能为空' }
  ]
})

// 修改其他属性的值
const emits = defineEmits(['update:modelValue', 'query'])
// 关闭
function handleClose() {
  emits('update:modelValue', false)
}
// 确定
const formRef = ref(null)
function handleConfirm() {
  formRef.value.validate(async (valid) => {
    if (valid) {
      // 获取id
      form.value.maintainRecordId = props.dialogRowValue.id
      // 判断是否为空 不是则将本地的数据共享的值存入form.value.maintainId
      if (store.getters.getGenerate != null) {
        const appointment = store.getters.getGenerate
        const app = JSON.parse(JSON.stringify(appointment))
        const gen = await getGenerate(app)
        form.value.maintainId = gen.data.data.id
      }
      console.log(form.value)
      await save(form.value)
      // props.dialogTitle === '生成明细' ? await save(form.value) : await modify(form.value)
      ElMessage({
        message: '更新成功',
        type: 'success'
      })
      // 上层组件重新获取数据
      emits('query')
      handleClose()
    } else {
      return false
    }
  })
}
</script>

<style class="scss" scoped>
::v-deep .el-input__prefix-inner {
  align-items: center;
}
</style>
